package com.tangkeyon.controller.current;

import com.tangkeyon.common.Const;
import com.tangkeyon.common.ResponseCode;
import com.tangkeyon.pojo.User;
import com.tangkeyon.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author: tangkeyon@gmail.com
 * @Date: 2019/5/21 22:03
 * @Version 1.0
 */
@Controller
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private IUserService iUserService;
    /**
     * 登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value ="login.do",method = RequestMethod.POST)
    @ResponseBody
    public ResponseCode<User> login(String username, String password, HttpSession session){
        ResponseCode<User> response = iUserService.login(username, password);
//        登陆成功后将用户的信息取出放到session中
        if(response.ifSuccess()){
             session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return  response;
    }

    /**
     * 登出
     * @param session
     * @return
     */
    @RequestMapping(value ="logout.do",method = RequestMethod.POST)
    @ResponseBody
    public ResponseCode<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return  ResponseCode.creatBySuccessMessage("成功退出");
    }

    /**
     * 获取登录用户的信息
     * @param session
     * @return
     */
    @RequestMapping(value ="get_user_info.do",method = RequestMethod.POST)
    @ResponseBody
  public ResponseCode<User> getUserInfo(HttpSession session){
      User user= (User) session.getAttribute(Const.CURRENT_USER);
      if(user !=null){
        return ResponseCode.creatBySuccess(user);
      }
        return  ResponseCode.createByErrorMessage("用户尚未登录");
  }

    /**
     * 注册
     * @param user
     * @return
     */
    @RequestMapping(value ="register_user.do",method = RequestMethod.POST)
    @ResponseBody
    public ResponseCode<String> registerUser(User user){ return iUserService.registerUser(user); }

    /**
     * 实时对参数进行非空存在性检查
     * @param value
     * @param type
     * @return
     */
    @RequestMapping(value ="check_valid.do",method = RequestMethod.POST)
    @ResponseBody
   public ResponseCode<String> checkValid(String value,String type){
        return iUserService.checkValid(value,type);
   }

    /**
     * 根据用户名返回对应密保问题
     * @param username
     * @return
     */
    @RequestMapping(value ="get_forget_question.do",method = RequestMethod.POST)
    @ResponseBody
   public ResponseCode<String> getForgetQuestion(String username){
        return  iUserService.getForgetQuestion(username);
   }

    /**
     *  检验用户回答的问题,并返回Token
     * @param username
     * @param question
     * @param answer
     * @return
     */
    @RequestMapping(value ="check_questionAnswer.do",method = RequestMethod.POST)
    @ResponseBody
    public ResponseCode<String> checkQuestionAnswer(String username,String question,String answer){
       return iUserService.checkQuestionAnswer(username,question,answer);
    }

    /**
     * 重置密码
     * @param username
     * @param password
     * @param token
     * @return
     */
    @RequestMapping(value ="resize_password.do",method = RequestMethod.POST)
    @ResponseBody
    public ResponseCode<String> resizePassword(String username,String password,String token){
        return iUserService.resizePassword(username,password,token);
    }

    /**
     * 登录状态下修改密码,在修改密码前要输入原始密码
     * @param passwordNew
     * @param session
     * @return
     */
    @RequestMapping(value ="change_password_login_state.do",method = RequestMethod.POST)
    @ResponseBody
    public ResponseCode<String> changePasswordLoginState(String passwordNew,String passwordOld,HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ResponseCode.createByErrorMessage("当前用户未登录");
        }
        return iUserService.changePasswordLoginState(passwordNew,passwordOld,user.getId());
    }

    /**
     * 登录状态下获取用户的信息
     * @param session
     * @return
     */
    @RequestMapping(value ="get_user_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ResponseCode<User> getUserInformation(HttpSession session){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null){
           return ResponseCode.createByErrorMessage("用户未登录，请先登录");
        }
        return iUserService.selectUserInfoById(currentUser.getId());
    }

    /**
     * 登录状态下修改用户的信息
     * @param session
     * @param user
     * @return
     */
    @RequestMapping(value ="update_user_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ResponseCode<String> updateUserInformation(HttpSession session,User user){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser==null){
            return ResponseCode.createByErrorMessage("用户未登录，请先登录");
        }
//        页面提交的修改字段没有Id,因此要把保存在session内的id取出来
        user.setId(currentUser.getId());
//        业务上不让修改username，一个好的办法就是不管页面有没有修改username，都把username改成原值
        user.setUsername(currentUser.getUsername());
        return iUserService.updateUserInformation(user);
    }

}
